LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2014, 01:21 AM   #1
yashfire007
Member
 
Registered: Jan 2013
Distribution: CentOS, RHEL 6.4
Posts: 79

Rep: Reputation: Disabled
{} vs () in a function


Hi to all,

function hai ()
{
printf "hai \n"
}

function hai ()
(
printf "hai \n"
)


Please explain me, what the difference it makes when using {} or () for body of the function.


Thanks in Advance
 
Old 08-25-2014, 01:43 AM   #2
jdkaye
LQ Guru
 
Registered: Dec 2008
Location: Westgate-on-Sea, Kent, UK
Distribution: Debian Testing Amd64
Posts: 5,465

Rep: Reputation: Disabled
Until you get a better answer, I'll say the following:
() enclose the arguments to the function.
{} enclose the body of the function; the series of operations performed by the function; what the function actually does.

jdk
 
Old 08-25-2014, 01:47 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
you can see when you execute a command to display defined functions (that is: type <name>):
Code:
# the first one will be:
hai () 
{ 
    printf "hai \n"
}


# the second one:
hai () 
{ 
    ( printf "hai \n" )
}
( ) will invoke an additional shell

Last edited by pan64; 08-25-2014 at 01:49 AM. Reason: additional info
 
Old 08-25-2014, 02:25 AM   #4
yashfire007
Member
 
Registered: Jan 2013
Distribution: CentOS, RHEL 6.4
Posts: 79

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
you can see when you execute a command to display defined functions (that is: type <name>):
Code:
# the first one will be:
hai () 
{ 
    printf "hai \n"
}


# the second one:
hai () 
{ 
    ( printf "hai \n" )
}
( ) will invoke an additional shell



So it means while using (), it uses subshells for every function enclosed by ()
But {} uses the same shell for all the functions enclosed by {}

Am i correct?
 
Old 08-25-2014, 02:29 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
yes, it looks like - but I have never used the version with ()
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript: Function that detects click and returns control to calling function slackaddict Programming 2 11-27-2013 02:53 PM
Windows Spawnl (nowait) like function in Linux... How to use Execl function? cheranj Programming 2 09-15-2011 08:01 PM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration